﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using DAL.Linq.EntityMapper;

namespace DAL.Linq
{
    public class LinqSYS_USER_Dao : ISYS_USER_Dao
    {
        #region IDao<SYS_USERInfo,int> Members

        /// <summary>
        /// Gets a list of all SYS_USERs.
        /// </summary>
        /// <returns>List of SYS_USERs.</returns>
        public IList<SYS_USER> GetAll()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a specific SYS_USER.
        /// </summary>
        /// <param name="userID">Unique UserID identifier.</param>
        /// <returns>SYS_USER.</returns>
        public SYS_USER GetById(int userID)
        {
            throw new NotImplementedException();
        }


        public SYS_USER GetLogin(string action, string usercode, string userpass)
        {
            SYS_USER userInfo = null;
            using (Invoice db = DataContextFactory.CreateContext())
            {
                try
                {
                    //userInfo = Mapper.ToBusinessObject(db.sp_Users(action,usercode,userpass).FirstOrDefault());
                }
                catch { }
                finally
                {
                    if (db != null)
                        db.Dispose();
                }
            }
            return userInfo;
        }

        
        #endregion

        #region IDao<SYS_USER,int> Members

        //IList<SYS_USER> IDao<SYS_USER, int>.GetAll()
        //{
        //    throw new NotImplementedException();
        //}

        SYS_USER IDao<SYS_USER, int>.GetById(int id)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ISYS_USER_Dao Members

        public bool UpdatePassword(int userID, string oldPass, string newPass)
        {
            int bln = 0;
            using (Invoice db = DataContextFactory.CreateContext())
            {
                try
                {
                    bln = db.sp_Sys_User_UpdatePassword(userID, oldPass, newPass);
                    if (bln == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch { return false; }
            }

        }

        public bool Insert_Sys_Users(string userCode, string userName, string userPass, bool IsUse)
        {
            int bln = 0;
            using (Invoice db = DataContextFactory.CreateContext())
            {
                try
                {
                    bln = db.sp_Sys_Users_Insert(userCode, userPass, userName, IsUse);
                    if (bln == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch { return false; }
            }
        }

        public bool Update_Sys_Users(int userID, string userCode, string userName, string userPass, bool isUse)
        {
            int bln = 0;
            using (Invoice db = DataContextFactory.CreateContext())
            {
                try
                {
                    bln = db.sp_Sys_Users_Update(userCode, userPass, userID, userName, isUse);
                    if (bln == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch { return false; }
            }
        }

        public bool Delete_Sys_Users(int userID)
        {
            int bln = 0;
            using (Invoice db = DataContextFactory.CreateContext())
            {
                try
                {
                    bln = db.Sp_Sys_Users_Delete(userID);
                    if (bln == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch { return false; }
            }
        }

        public IList<SYS_USER> GetAll_Users()
        {
            List<SYS_USER> lst = null;
            using (Invoice db = DataContextFactory.CreateContext())
            {
                try
                {
                    lst = db.Sp_Sys_Users_GetAll().Select(p => Mapper.ToBusinessObject(p)).ToList();
                }
                catch { }
                finally
                {
                    if (db != null)
                        db.Dispose();
                }
            }
            return lst;
        }

        public SYS_USER GetLogin(string userCode, string userPass)
        {
            SYS_USER lst = null;
            using (Invoice db = DataContextFactory.CreateContext())
            {
                try
                {
                    //lst = db.Sp_Sys_Users_GetLogin(userCode,userPass).Select(p => Mapper.ToBusinessObject(p)).ToList();
                    lst = Mapper.ToBusinessObject(db.Sp_Sys_Users_GetLogin(userCode, userPass).FirstOrDefault());
                }
                catch { }
                finally
                {
                    if (db != null)
                        db.Dispose();
                }
            }
            return lst;
        }
        #endregion
        
    }
}
